package com.baoyi.service;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.util.Log;

import com.baoyi.content.content;
import com.baoyi.doamin.Comments;
import com.baoyi.pooke.BaoyiApplication;
import com.baoyi.utils.Connection;
import com.baoyi.utils.Connection.Method;
import com.baoyi.utils.HttpConnection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class CommentsServiceImpl {

	public void add(Comments item) {
		String url = content.server + "AddComments";
		Connection conn = HttpConnection.connect(url);
		Gson gson = new Gson();
		conn.data("datas", gson.toJson(item, Comments.class));
		conn.method(Method.POST);
		String messages = null;
		try {
			messages = conn.execute().body();
			Log.d(BaoyiApplication.getInstance().TAG, messages);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public List<Comments> findbypid(Integer pid) {
		List<Comments> items = new ArrayList<Comments>();
		try {
			String url = content.server + "ListComments";
			Connection conn = HttpConnection.connect(url);
			conn.data("workitemid", pid.toString());
			conn.method(Method.GET);
			String messages = null;
			try {
				messages = conn.execute().body();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			Gson gson = new Gson();
			Type listType = new TypeToken<LinkedList<Comments>>() {
			}.getType();
			items = gson.fromJson(messages, listType);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return items;

	}
}
